package com.yyk.yykywd.mvvm.ui

import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.orhanobut.logger.Logger
import com.yyk.yykywd.*
import com.yyk.yykywd.bean.BaseBean
import com.yyk.yykywd.mvvm.base.LoadRegionData
import com.yyk.yykywd.mvvm.entity.MeetingDetailBean
import com.yyk.yykywd.mvvm.view_model.ModifyMeetingInvitationViewModel
import com.yyk.yykywd.util.TimeUtil
import com.yyk.yykywd.util.ToastUtil
import kotlinx.android.synthetic.main.activity_modify_meeting_invitation.*
import kotlinx.android.synthetic.main.rl_bottom.*
import java.util.*


class ModifyMeetingInvitationActivity : LoadRegionData() {
    private lateinit var viewModel: ModifyMeetingInvitationViewModel

    override fun initView() {
        setBackArrow()
        tv_execute.text = "保存"

        viewModel = ViewModelProviders.of(this).get(ModifyMeetingInvitationViewModel::class.java)
        viewModel.type = intent.getStringExtra(TYPE)
        viewModel.id = intent.getStringExtra(ID)

        if (viewModel.type == MODIFY) {
            setTitle("修改会议邀约")
        } else {
            setTitle("创建会议邀约")
        }
    }

    override fun setListener() {
        super.setListener()

        tv_meeting_region.setOnClickListener {
            if (isLoaded) {
                showPickerView()
            }
        }
        tv_start_time.setOnClickListener {
            showTimePicker(mActivity,
                    showYear = true,
                    showMonth = true,
                    showDay = true,
                    showHour = true,
                    showMinute = true,
                    showSecond = false,
                    startDate = Calendar.getInstance(),
                    endDate = viewModel.endDate,
                    pattern = TimeUtil.YYYY_MM_DD_HH_MM) {
                tv_start_time.text = it
            }
        }

        viewModel.mMeetingDetail.observe(this, Observer<MeetingDetailBean> {
            with(it.data) {
                et_meeting_name.setText(name)
                et_meeting_name.setSelection(name.length)
                tv_meeting_region.text = region
                et_meeting_site.setText(site)
                tv_start_time.text = getCommonTime(start_time)
                et_meeting_summary.setText(remark)
                this@ModifyMeetingInvitationActivity.province_id = province_id
                this@ModifyMeetingInvitationActivity.city_id = city_id.toString()
                this@ModifyMeetingInvitationActivity.county_id = area_id.toString()
            }

        })
        viewModel.base.observe(this,Observer<BaseBean>{
            ToastUtil.showShort(it.msg)
            if (it.code == 200) {
                finish()
            }
        })

        tv_execute.setOnClickListener {
            val name = getTextString(et_meeting_name)
            val site = getTextString(et_meeting_site)
            val startTime = getTextString(tv_start_time)
            if (name.isBlank()) {
                ToastUtil.showShort("请填写会议名称")
                return@setOnClickListener
            }
            if (getTextString(tv_meeting_region) == "请选择") {
                ToastUtil.showShort("请选择会议地区")
                return@setOnClickListener
            }
            if (site.isBlank()) {
                ToastUtil.showShort("请填写会议地点")
                return@setOnClickListener
            }
            if (startTime == "请选择") {
                ToastUtil.showShort("请选择开始时间")
                return@setOnClickListener
            }
            viewModel.addOrModifyMeeting(name = name,
                    province_id = province_id.toString(),
                    city_id = city_id.toString(),
                    area_id = county_id.toString(),
                    site = site,
                    start_time = transToTimeStamp(startTime,TimeUtil.YYYY_MM_DD_HH_MM).toString(),
                    remark = getTextString(et_meeting_summary))
        }
    }

    override fun initData() {
        //加载省市级选择器
        mHandler.sendEmptyMessage(MSG_LOAD_DATA)
        if (viewModel.type == MODIFY) {
            viewModel.getMeetingDetail()
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_modify_meeting_invitation
    }


    private fun showPickerView() { // 弹出选择器
        val pvOptions = showOptionsPicker(mActivity) { options1: Int, options2: Int, options3: Int ->
            val tx = options1Items[options1].pickerViewText.toString() + "—" +
                    options2Items[options1][options2] + "—" +
                    options3Items[options1][options2][options3]
            tv_meeting_region.text = tx
            province_id = index1Items[options1].name_id
            city_id = index2Items[options1][options2]
            county_id = index3Items[options1][options2][options3]
            Logger.e("省市区$province_id,$city_id,$county_id")
        }
        pvOptions.setSelectOptions(0, 0, 0)
        pvOptions.setPicker(options1Items as List<Any>, options2Items as List<MutableList<Any>>, options3Items as List<MutableList<MutableList<Any>>>) //三级选择器
        pvOptions.show()
    }
}
